﻿using System;
using System.Collections.Generic;
using System.Linq;
using Trio.Motor;
using EastForceCondensor.CommCtrl;
using System.Timers;

namespace MCorder.Control
{
    public struct ControlDataSet
    {
        public double cur;
        public double pos;
        public double speed;
        public double voltage;
        public int num_pos;
    }
    public class MCorderControl
    {
        private Motor MotorControl = new Motor();//Trio.Motor
        private DataSaver DataSaver = null;//MCorder.Control
        private CommMgmt commCtrl = new CommMgmt();//721.CommCtrl

        public float coff = 1.0f;

        public delegate void delPlaneScanCallback();

        public bool Init()
        {
            if (commCtrl.Connect() && MotorControl.Init())
            {
                Init_ADtimer();
                return true;
            }
            else
            {
                return false;
            }
        }
        public double num = 0;

        private bool saveFlag = false;

        double current = 0;
        double position = 0;
        double speed = 0;
        int num_return = 20;

        double old;

        public List<float> Cur_list = new List<float>();
        public List<float> Pos_list = new List<float>();
        public List<float> Speed_list = new List<float>();

        Timer ADtimer = new Timer();
        public void Init_ADtimer()
        {
            ADtimer.Interval = 39; //这个时间单位毫秒,比如10秒，就写10000 
            ADtimer.Elapsed += new ElapsedEventHandler(ADtimer_Elapsed);
            ADtimer.Start();
        }
        public void Stop_ADtimer()
        {
            ADtimer.Stop();

        }

        ControlDataSet devData = new ControlDataSet();
        void ADtimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            GetData(ref devData);
        }

        public bool GetData(ref ControlDataSet devData)
        {
            ADDataSet aveData = new ADDataSet();
            if (commCtrl.GetAverageData(ref aveData))
            {
                current = -aveData.voltageArray[2];
            }
            position = 0;

            devData.num_pos = MotorControl.distance_list.Count();

            if (MotorControl.distance_list.Count() > 0)
            {
                position = MotorControl.distance_list[0];
                MotorControl.distance_list.RemoveAt(0);
            }

            if (MotorControl.GetVelocity() > 0)
            {
                speed = MotorControl.GetVelocity();
            }

            if (distance_base > 0.1 && num_return > 0)
            {
                num_return--;
                return false;
            }

            position = position + distance_base;
            if (current != old) //&& position > 0)
            {
                devData.cur = (current - volt_base) / coff;
                devData.pos = position;
                devData.speed = speed;
                devData.voltage = current;
                Pos_list.Add((float)devData.pos);
                Cur_list.Add((float)devData.cur);
                Speed_list.Add((float)speed);
                old = current;
                if (position > 0.01)
                {
                    if (saveFlag)
                    {
                        DataSaver.SaveData(devData);
                        MotorControl.distance_list.Clear();
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        private double volt_base = 0.0;

        private double distance_base = 0.0;
        public void setCoff(float cc)
        {
            coff = cc;
        }

        private double getVbase()
        {
            float[] add = new float[5];
            float v0 = 0;
            if (current != 0)
            {
                v0 += (float)current;
            }
            return v0;
        }
        string lastFileName = "";

        string OtherName = "";
        string OtherName1 = "";
        public string OtherFileName()
        {
            OtherName1 = OtherName;
            return OtherName1;
        }
        public void StartRoll_Plane(string fileName1, int direction, double speed, double tension, bool followlasttime, bool OpenfromExtern, double ad_base, List<float> position, List<float> cur, string path_extern)
        {
            if (followlasttime)
            {
                if (OpenfromExtern == true)
                {
                    distance_base = Convert.ToDouble(position[position.Count() - 1]);
                    OpenfromExtern = false;
                    if (fileName1 != "")
                    {
                        DataSaver = new DataSaver(fileName1, path_extern, true, fileName1, true);
                        OtherName = path_extern;
                    }
                    else
                    {
                        string Name = DateTime.Now.ToString("yyyyMMdd-HH-mm-ss");
                        DataSaver = new DataSaver(Name, path_extern, true, DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"), true);
                        volt_base = ad_base;
                        OtherName = path_extern;
                    }
                }
                else
                {
                    DataSaver = new DataSaver(lastFileName, null, false, lastFileName, true);
                    OtherName = "data\\" + lastFileName + ".txt";
                }
            }

            if (followlasttime == false)
            {
                distance_base = 0;
                ad_base = getVbase();//获取基底数据
                volt_base = ad_base;

                string Name = DateTime.Now.ToString("yyyyMMdd-HH-mm-ss");
                if (fileName1 == "")
                {
                    DataSaver = new DataSaver(Name, null, false, Name, false);
                    lastFileName = Name;
                    OtherName = "data\\" + Name + ".txt";
                }
                else
                {
                    DataSaver = new DataSaver(fileName1 + "_" + Name, path_extern, false, fileName1, false);
                    lastFileName = fileName1 + "_" + Name;
                    OtherName = "data\\" + fileName1 + "_" + Name + ".txt";
                }
                DataSaver.SaveHeader(coff, ad_base, speed);

                MotorControl.SetZeroEp();
            }

            saveFlag = true;

            if (direction == 0)//right
            {
                MotorControl.DIR = 1;
                MotorControl.setclutch(tension, 0);
            }
            else
            {
                MotorControl.DIR = 0;
                MotorControl.setclutch(0, tension);
            }
            MotorControl.speed_Aim = speed;

            MotorControl.start();
        }
        public void StopRoll()//长度到达、手动、关闭程序时运行
        {
            saveFlag = false;//保存-否
            MotorControl.stop();
        }
        public void StopComm()
        {
            Stop_ADtimer();
            MotorControl.End();
            commCtrl.StopReceive();
            commCtrl.Disconnect();
        }
        public void StopDataSaving()
        {
            if (DataSaver != null)
            {
                DataSaver.SaveEnd();
            }
            Pos_list.Clear();
            Cur_list.Clear();
            Speed_list.Clear();
        }
    }
}